# _*_ coding: UTF-8 _*_
# @Time     : 2021/11/1 17:30
# @Author   : 郑鲁明
# @Site     : http://www.hzdledu.cn/
# @File     : prac.py
# @Software : PyCharm
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys


def foo1():
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('https://www.jd.com/')

    driver.find_element_by_id('key').send_keys('显示器')
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="search"]/div/div[2]/button/i').click()
    time.sleep(2)
    driver.find_element_by_xpath('/html').send_keys(Keys.PAGE_DOWN)

    for i in range(1, 6):
        driver.find_element_by_xpath(f'//*[@id="J_goodsList"]/ul/li[{i}]/div/div[1]/a/img').click()
        time.sleep(1)
    handles = driver.window_handles
    for handle in handles:
        driver.switch_to.window(handle)
        url = driver.current_url  # 获取url
        if url == 'https://item.jd.com/100005855774.html':
            break
        time.sleep(1)
    driver.find_element_by_xpath('//*[@id="choose-attr-1"]/div[2]/div[4]/a').click()
    driver.find_element_by_xpath('//*[@id="choose-service"]/div[2]/div/div[1]/div[1]')
    driver.find_element_by_id('buy-num').clear()
    driver.find_element_by_id('buy-num').send_keys('5')
    time.sleep(2)
    driver.find_element_by_id('InitCartUrl').click()
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/div/div[3]/a').click()
    driver.find_element_by_id('loginname').clear()
    driver.find_element_by_id('loginname').send_keys('text1')
    driver.find_element_by_id('nloginpwd').clear()
    driver.find_element_by_id('nloginpwd').send_keys('123456')

    time.sleep(10)
    driver.quit()


if __name__ == '__main__':
    foo1()
